let wxhttp2 = function (newAxiosConfig?: axiosConfig) {

  let paramsDefaultConfig: axiosDefaultConfig | null = {
    url: '',
    baseUrl: '',
    method: 'GET',
    timeout: 0,
    transformRequest: undefined,
    transformResponse: undefined,
    adapter: undefined,
    responseType: 'json',
    handleInterceptRequest: undefined,
    handleInterceptRequestErr: undefined,
    handleInterceptResponse: undefined,
    handleInterceptResponseErr: undefined
  }

  function http (params: axiosConfig) {
    params = Object.assign({}, http.default, params)

  }
  // 设置默认值
  http.default = Object.assign({}, paramsDefaultConfig, (newAxiosConfig || {}))
  paramsDefaultConfig = null

  // 请求的方法
  async function request (config: axiosConfig): Promise<responseData> {
    if (config.handleInterceptRequest) {
      config = await config.handleInterceptRequest(config)
    }
    config.baseUrl && (config.url = config.baseUrl + config.url)
    return new Promise((reso, rej) => {
      wx.request({
        ...config,
        method: (<string>config.method).toUpperCase(),
        data: {...config.data, ...config.params},
        success (res: responseData) {
          if (config.handleInterceptResponse) {
            Promise.resolve(config.handleInterceptResponse(res)).then(res => {
              reso(res)
            }).catch(err => {
              config.handleInterceptResponseErr && (config.handleInterceptResponseErr(err))
            })
          } else {
            reso(res)
          }
        },
        fail (err: Error) {
          if (config.handleInterceptRequestErr) {
            Promise.resolve(config.handleInterceptRequestErr(err)).then(res => {
              rej(res)
            })
          } else {
            rej(err)
          }
        },
        complete () {
          console.log(`one request finished`)
        }
      })
    })
  }

  // 请求方法的三种类型
  let requestMethods = {
    type1 (url: string, config?: axiosConfig) {
      return request(Object.assign({}, http.default, (config || {}), {url, method: 'GET'}))
    },
    type2 (type: string) {
      return function (url: string, config?: axiosConfig) {
        return request(Object.assign({}, http.default, (config || {}), {url, method: type.toUpperCase()}))
      }
    },
    type3 (type: string) {
      return function (url: string, data?: anyObj, config?: axiosConfig) {
        return request(Object.assign({}, http.default, (config || {}), {url, method: type.toUpperCase(), data: data || {}}))
      }
    }
  }

  // 处理各个方法
  http.get = requestMethods.type1
  http.delete = requestMethods.type2('delete')
  http.head = requestMethods.type2('head')
  http.options = requestMethods.type2('options')
  http.put = requestMethods.type3('put')
  http.post = requestMethods.type3('post')
  http.patch = requestMethods.type3('patch')

  // 设置拦截方法

  http.interceptors = {
    request: {
      use (handleRequest: handleRequest, handleError: handleInterceptErr) {
        http.default.handleInterceptRequest = handleRequest
        handleError && (http.default.handleInterceptRequestErr = handleError)
      }
    },
    response: {
      use (handleResponse: handleResponse, handleErr?: handleInterceptErr) {
        http.default.handleInterceptResponse = handleResponse
        handleErr && (http.default.handleInterceptResponseErr = handleErr)
      }
    }
  }

  // 创建新的实例的方法
  http.create = wxhttp2

  return http
}

export default wxhttp2()
